/*     */ package com.hp.bon.logreverse;
/*     */ 
/*     */ import com.hp.bon.sgw.util.Comutils;
/*     */ import java.io.File;
/*     */ import java.io.FilenameFilter;
/*     */ import java.text.ParseException;
/*     */ import java.util.Arrays;
/*     */ import java.util.Calendar;
/*     */ import java.util.Comparator;
/*     */ import java.util.regex.Matcher;
/*     */ import java.util.regex.Pattern;
/*     */ import java.util.regex.PatternSyntaxException;
/*     */ import org.slf4j.Logger;
/*     */ import org.slf4j.LoggerFactory;
/*     */ 
/*     */ public class LogDirScan extends Thread
/*     */ {
/*  24 */   private static final Logger LOG = LoggerFactory.getLogger(LogDirScan.class);
/*     */ 
/*  26 */   private volatile boolean exit = false;
/*  27 */   private volatile boolean canExit = true;
/*     */   private String logPath;
/*  44 */   private String filePrefix = "SGW_MAIN.";
/*  45 */   private String fileSuffix = ".log";
/*  46 */   private String dirRegex = "[0-9]{8}";
/*     */   protected ILogFileRead logFileRead;
/*  49 */   protected String curDay = null;
/*  50 */   protected int curSeq = -1;
/*     */ 
/*     */   public LogDirScan()
/*     */   {
/*  21 */     super("LogDirScanThread");
/*     */   }
/*     */ 
/*     */   public void exit()
/*     */   {
/*  30 */     LOG.info("LogDirScan thread will exit.");
/*  31 */     this.exit = true;
/*  32 */     interrupt();
/*  33 */     while (!this.canExit)
/*     */       try {
/*  35 */         LOG.info("wait LogDirScan Thread exit.");
/*  36 */         Thread.sleep(5000L);
/*     */       }
/*     */       catch (InterruptedException localInterruptedException) {
/*     */       }
/*  40 */     LOG.info("LogDirScan thread exit.");
/*     */   }
/*     */ 
/*     */   public void run()
/*     */   {
/*  54 */     String startDay = null;
/*  55 */     String endDay = null;
/*  56 */     int startSeq = -1;
/*  57 */     int endSeq = -1;
/*     */ 
/*  59 */     findFileByStartAndEnd(startDay, startSeq, endDay, endSeq);
/*     */ 
/*  62 */     if (this.curDay == null) {
/*  63 */       this.curDay = Comutils.formatTime(System.currentTimeMillis(), "yyyyMMdd");
/*  64 */       this.curSeq = -1;
/*     */     }
/*  66 */     findFileBySeqWithBlock(this.curDay, this.curSeq + 1);
/*     */   }
/*     */ 
/*     */   public void findFileByStartAndEnd(String startDay, int startSeq, String endDay, int endSeq)
/*     */   {
/*     */     try
/*     */     {
/*  81 */       File folderDir = new File(this.logPath);
/*  82 */       if ((!folderDir.exists()) || (!folderDir.isDirectory())) {
/*  83 */         LOG.error("program exited," + folderDir.getAbsolutePath() + " is not exist or is not a directory.");
/*  84 */         System.exit(-1);
/*     */       }
/*  86 */       File[] folders = folderDir.listFiles(new LogDirFilter());
/*  87 */       Arrays.sort(folders, new CompratorByFileDay());
/*     */ 
/*  89 */       for (int i = 0; i < folders.length; i++)
/*     */       {
/*     */         try
/*     */         {
/*  93 */           if (!folders[i].isDirectory()) {
/*  94 */             LOG.info(folders[i].getAbsolutePath() + " is not dir，skipped");
/*     */           }
/*  98 */           else if ((startDay != null) && (folders[i].getName().compareTo(startDay) < 0)) {
/*  99 */             LOG.info("skip folder for folder=" + folders[i].getName());
/*     */           }
/*     */           else
/*     */           {
/* 103 */             this.curDay = folders[i].getName();
/* 104 */             if (this.curDay.equals(startDay))
/*     */             {
/* 106 */               this.curSeq = (startSeq - 1);
/*     */             }
/*     */             else
/*     */             {
/* 110 */               this.curSeq = -1;
/*     */             }
/* 112 */             File curDir = new File(this.logPath + File.separator + folders[i].getName());
/* 113 */             File[] files = curDir.listFiles(new LogFileFilter());
/* 114 */             Arrays.sort(files, new CompratorByFileNum());
/*     */ 
/* 116 */             for (int j = 0; j < files.length; j++)
/*     */             {
/*     */               try
/*     */               {
/* 120 */                 if (!files[j].getName().startsWith(this.filePrefix)) {
/* 121 */                   LOG.info("file name not match,skip file=" + files[j].getName());
/*     */                 }
/*     */                 else
/*     */                 {
/* 125 */                   int fileSeq = getFileSeq(files[j].getName());
/*     */ 
/* 127 */                   if (fileSeq <= this.curSeq) {
/* 128 */                     LOG.info("skip file for seq =" + files[j].getName());
/*     */                   }
/*     */                   else
/*     */                   {
/* 132 */                     if ((endDay != null) && (this.curDay.equals(endDay)))
/*     */                     {
/* 134 */                       if (fileSeq > endSeq)
/*     */                       {
/* 136 */                         LOG.info("program exited,because processed to endDay=" + endDay + " and endSeq=" + endSeq + ",file=" + files[j].getName());
/* 137 */                         return;
/*     */                       }
/*     */                     }
/* 140 */                     processFile(files[j]);
/* 141 */                     this.curSeq = fileSeq;
/*     */                   }
/*     */                 }
/*     */               } catch (Exception e) {
/* 145 */                 LOG.warn("When process file  " + files[j].getAbsolutePath() + " error occur.", e);
/*     */               }
/*     */             }
/*     */           }
/*     */ 
/*     */         }
/*     */         catch (Exception e)
/*     */         {
/* 153 */           LOG.error("When scan " + folders[i].getAbsolutePath() + " error occur.", e);
/*     */         }
/*     */ 
/*     */       }
/*     */ 
/*     */     }
/*     */     catch (Exception e)
/*     */     {
/* 161 */       LOG.error("findFileByStartAndEnd error occur.", e);
/*     */     }
/*     */   }
/*     */ 
/*     */   public void findFileBySeqWithBlock(String dayStr, int fileSeq)
/*     */   {
/* 167 */     while (!this.exit) {
/* 168 */       long curTime = System.currentTimeMillis();
/* 169 */       String today = Comutils.formatTime(curTime, "yyyyMMdd");
/* 170 */       String handleFileName = this.logPath + File.separator + dayStr + File.separator + getFileName(dayStr, fileSeq);
/*     */       try
/*     */       {
/* 174 */         File file = new File(handleFileName);
/*     */ 
/* 176 */         if (file.exists()) {
/* 177 */           processFile(file);
/* 178 */           fileSeq++;
/*     */         } else {
/* 180 */           String nextDay = getNextDay(dayStr);
/* 181 */           if (dayStr.equals(today)) {
/* 182 */             LOG.info(file.getAbsolutePath() + " hasn't reach,sleep 60s.");
/*     */             try {
/* 184 */               Thread.sleep(60000L);
/*     */             } catch (InterruptedException e) {
/* 186 */               e.fillInStackTrace();
/*     */             }
/*     */           }
/* 189 */           else if (nextDay.compareTo(today) <= 0) {
/* 190 */             LOG.info(">>>>>>>>>>>>>>>> process " + dayStr + " end, start read " + nextDay);
/* 191 */             dayStr = nextDay;
/* 192 */             fileSeq = 0;
/*     */           }
/*     */         }
/*     */       }
/*     */       catch (Exception e)
/*     */       {
/* 198 */         LOG.error("When process file  " + handleFileName + " error occur.", e);
/*     */       }
/*     */     }
/*     */   }
/*     */ 
/*     */   public void processFile(File file)
/*     */   {
/* 210 */     this.logFileRead.readAFile(file);
/*     */   }
/*     */ 
/*     */   protected String getFileDay(String fileName) {
/* 214 */     String[] fileArr = fileName.split("\\.");
/*     */     try {
/* 216 */       return fileArr[1]; } catch (Exception e) {
/*     */     }
/* 218 */     return null;
/*     */   }
/*     */ 
/*     */   protected int getFileSeq(String fileName)
/*     */   {
/* 223 */     String[] fileArr = fileName.split("\\.");
/*     */     try {
/* 225 */       return Integer.valueOf(fileArr[2]).intValue(); } catch (Exception e) {
/*     */     }
/* 227 */     return -1;
/*     */   }
/*     */ 
/*     */   private String getNextDay(String curDay)
/*     */   {
/*     */     try {
/* 233 */       long curTime = Comutils.parseStringTime(curDay, "yyyyMMdd");
/* 234 */       Calendar c = Calendar.getInstance();
/* 235 */       c.setTimeInMillis(curTime);
/* 236 */       c.set(5, c.get(5) + 1);
/* 237 */       return Comutils.formatTime(c.getTimeInMillis(), "yyyyMMdd");
/*     */     } catch (ParseException e) {
/* 239 */       LOG.error("When getNextDay(" + curDay + ") error occur.", e);
/*     */     }
/* 241 */     return curDay;
/*     */   }
/*     */ 
/*     */   private String getFileName(String day, int num) {
/* 245 */     return this.filePrefix + day + "." + num + this.fileSuffix;
/*     */   }
/*     */ 
/*     */   public void setLogPath(String logPath)
/*     */   {
/* 321 */     this.logPath = logPath;
/*     */   }
/*     */   public void setDirRegex(String dirRegex) {
/* 324 */     this.dirRegex = dirRegex;
/*     */   }
/*     */   public void setLogFileRead(ILogFileRead logFileRead) {
/* 327 */     this.logFileRead = logFileRead;
/*     */   }
/*     */   public void setFilePrefix(String filePrefix) {
/* 330 */     this.filePrefix = filePrefix;
/*     */   }
/*     */   public void setFileSuffix(String fileSuffix) {
/* 333 */     this.fileSuffix = fileSuffix;
/*     */   }
/*     */ 
/*     */   static class CompratorByFileDay
/*     */     implements Comparator<File>
/*     */   {
/*     */     public int compare(File f1, File f2)
/*     */     {
/* 250 */       long diff = f1.getName().compareTo(f2.getName());
/* 251 */       if (diff > 0L)
/* 252 */         return 1;
/* 253 */       if (diff == 0L) {
/* 254 */         return 0;
/*     */       }
/* 256 */       return -1;
/*     */     }
/*     */ 
/*     */     public boolean equals(Object obj) {
/* 260 */       return true;
/*     */     }
/*     */   }
/*     */ 
/*     */   static class CompratorByFileNum implements Comparator<File> {
/*     */     public int compare(File f1, File f2) {
/* 266 */       int num1 = Integer.valueOf(f1.getName().split("\\.")[2]).intValue();
/* 267 */       int num2 = Integer.valueOf(f2.getName().split("\\.")[2]).intValue();
/* 268 */       long diff = num1 - num2;
/* 269 */       if (diff > 0L)
/* 270 */         return 1;
/* 271 */       if (diff == 0L) {
/* 272 */         return 0;
/*     */       }
/* 274 */       return -1;
/*     */     }
/*     */ 
/*     */     public boolean equals(Object obj) {
/* 278 */       return true;
/*     */     }
/*     */   }
/*     */ 
/*     */   class LogDirFilter implements FilenameFilter {
/*     */     LogDirFilter() {
/*     */     }
/*     */ 
/*     */     public boolean accept(File dir, String fname) {
/* 287 */       boolean bl = false;
/* 288 */       Pattern pat = null;
/*     */       try
/*     */       {
/* 291 */         pat = Pattern.compile(LogDirScan.this.dirRegex);
/*     */       }
/*     */       catch (PatternSyntaxException e)
/*     */       {
/* 295 */         LogDirScan.LOG.error("Patten cannot compile:" + LogDirScan.this.dirRegex);
/*     */       }
/* 297 */       Matcher m = pat.matcher(fname);
/* 298 */       if (m.matches())
/* 299 */         bl = true;
/* 300 */       return bl;
/*     */     }
/*     */   }
/*     */ 
/*     */   class LogFileFilter implements FilenameFilter {
/*     */     LogFileFilter() {
/*     */     }
/*     */ 
/* 308 */     public boolean accept(File dir, String fname) { boolean retVal = false;
/* 309 */       if ((fname != null) && (fname.length() > 0))
/*     */       {
/* 311 */         if ((fname.toUpperCase().startsWith(LogDirScan.this.filePrefix)) && 
/* 312 */           (fname.toLowerCase().endsWith(LogDirScan.this.fileSuffix))) {
/* 313 */           retVal = true;
/*     */         }
/*     */       }
/* 316 */       return retVal;
/*     */     }
/*     */   }
/*     */ }

/* Location:           C:\Users\think\Desktop\sgw_2.1.jar
 * Qualified Name:     com.hp.bon.logreverse.LogDirScan
 * JD-Core Version:    0.6.1
 */